package com.jx.controller;

import com.jx.service.CarService;
import com.jx.service.JyxmztService;
import com.jx.service.PhotoRecordService;
import com.jx.vo.CarVo;
import com.jx.vo.CatChPhoneVo;
import com.jx.vo.XmlVo;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

@RestController
public class CarController {
    @Autowired
    CarService carService;
    @Autowired
    JyxmztService jyxmztService;
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private PhotoRecordService photoRecordService;
    @Value("${webUrl:http://192.168.11.200:8080/VehicleCheck.asmx}")
    String webUrl;
    @Value("${webNameSpace:http://tempuri.org/}")
    String webNameSpace;
    @Value("${webMethodName:UploadObjectOut}")
    String webMethodName;
    String xmlTemplate = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
            "  <soap:Body>" +
            "    <UploadObjectOut xmlns=\"http://tempuri.org/\">" +
            "      <nSignal>%s</nSignal>" +
            "      <strXml>%s</strXml>" +
            "      <strIP></strIP>" +
            "    </UploadObjectOut>" +
            "  </soap:Body>" +
            "</soap:Envelope>";

    String contentXmlTemplate = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><vehispara><jylsh>%s</jylsh><jyjgbh>%s</jyjgbh><jcxdh>1</jcxdh><jycs>%s</jycs><hphm>%s</hphm><hpzl>02</hpzl><clsbdh>LSVNV4BR2GN282890</clsbdh><jyxm>B6</jyxm><sbdh>2</sbdh></vehispara></root>";

    @PostMapping("/cars")
    public Object list(@Valid CarVo carVo) {
        return jyxmztService.getCars(carVo);
    }

    @PostMapping("photo")
    public Object catchPhoto(@Valid CatChPhoneVo catChPhoneVo) throws IOException {        XmlVo xmlVo = new XmlVo();
        xmlVo.setId("6");
        xmlVo.setXmlValue(StringEscapeUtils.escapeXml(catChPhoneVo.getXml()));
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("text/xml;charset=UTF-8");
        headers.setContentType(type);
        headers.set("SOAPAction", "http://tempuri.org/UploadObjectOut");
        HttpEntity<String> stringHttpEntity = new HttpEntity<>(String.format(xmlTemplate, xmlVo.getId(), xmlVo.getXmlValue()), headers);
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(webUrl, stringHttpEntity, String.class);
        return stringResponseEntity;
    }

    @RequestMapping("/")
    public void index(HttpServletResponse response) {
        try {
            response.sendRedirect("/index.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/download/{jylsh}")
    public void downloadPermissionPhoto(HttpServletResponse response, @PathVariable(name = "jylsh") String jylsh, HttpServletRequest request) {
        String photoPath = photoRecordService.getPhotoPath(jylsh);
        if (photoPath == null) {
            try {
                response.sendRedirect("/errorPhoto_1.jpg");
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        File file = new File(photoPath);
        if (!file.exists() || file.isDirectory()) {
            try {
                response.sendRedirect("/errorPhoto_1.jpg");
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try (FileInputStream fileInputStream = new FileInputStream(file)){
            IOUtils.copy(fileInputStream, response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
